package com.ztesoft.sca.dao;

import com.ztesoft.sca.model.ImButtonDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hxz
 */
@Component
public class ImButtonDaoImpl implements ImButtonDao{
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public ImButtonDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    @Override
    public List<ImButtonDto> listImButton(ImButtonDto imButton, int from, int size) throws Exception {
    	StringBuffer sqlBuf = new StringBuffer();
    	List qryList=new ArrayList();
    	sqlBuf.append(" SELECT * FROM (SELECT ROWNUM rn,BUTTON_ID, BUTTON_CODE, BUTTON_NAME, COMMENTS ")
        	  .append(" FROM (SELECT BUTTON_ID, BUTTON_CODE, BUTTON_NAME, COMMENTS FROM IM_BUTTON WHERE 1=1 ");
    	  
    	  if(imButton.getButtonId()!=null){
	  		  sqlBuf.append(" AND BUTTON_ID=? ");   
	  		  qryList.add(imButton.getButtonId());
	  	  }
	  	  if(imButton.getButtonCode()!=null && !"".equals(imButton.getButtonCode())){
	  		  sqlBuf.append(" AND BUTTON_CODE=? ");   
	  		  qryList.add(imButton.getButtonCode());
	  	  }
	  	  if(imButton.getButtonName()!=null && !"".equals(imButton.getButtonName())){
	  		  sqlBuf.append(" AND BUTTON_NAME=? ");
	  		  qryList.add(imButton.getButtonName());
	  	  }
    	
	  	sqlBuf.append(" ) WHERE ROWNUM<=? ORDER BY BUTTON_ID asc) t2 WHERE T2.rn >=? ");
    	
        if (from < 0) {
            from = Integer.MAX_VALUE; 
        }
        
        qryList.add(size);
        qryList.add(from);
        Object[] param =qryList.toArray(); 
        List<ImButtonDto> list = this.jdbcTemplate.query(sqlBuf.toString(),param,BeanPropertyRowMapper.newInstance(ImButtonDto.class));
        return list;
    }
    
    @Override
    public void insertImButton(ImButtonDto imButton) throws Exception {
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append(" insert into IM_BUTTON ")                
              .append(" (BUTTON_ID, BUTTON_CODE, BUTTON_NAME, COMMENTS) " )
              .append(" VALUES (?,?,?,?) ");
        Object[] param = new Object[]{imButton.getButtonId(),imButton.getButtonCode(),imButton.getButtonName(),imButton.getComments()};
        this.jdbcTemplate.update(sqlBuf.toString(),param);
    }

	@Override
	public void delImButton(ImButtonDto imButton) throws Exception {
        String sqlStr = " delete from IM_BUTTON where BUTTON_ID=? ";
        Object[] param = new Object[]{imButton.getButtonId()};
        this.jdbcTemplate.update(sqlStr,param);
    }

	@Override
	public void modifyImButton(ImButtonDto imButton) throws Exception {
		StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append(" update IM_BUTTON set BUTTON_CODE=? , BUTTON_NAME=?, COMMENTS=? WHERE BUTTON_ID=? ");   
        Object[] param = new Object[]{imButton.getButtonCode(),imButton.getButtonName(),imButton.getComments(),imButton.getButtonId()};
        this.jdbcTemplate.update(sqlBuf.toString(),param);
    }

	
}
